<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login-form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来的硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="login"
              class="login-btn"
              type="primary"
              size="default"
              :loading="loading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { Lock, User } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
// 引用用户相关的小仓库
import useUserStore from '@/store/modules/user'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time'
// 收集账号与密码的数据
let loginForm = reactive({ username: 'admin', password: '111111' })
// 定义变量设置loading
let loading = ref(false)
let loginForms = ref()

const useStore = useUserStore()
let router = useRouter()
let route = useRoute()

// 登录按钮回调
const login = async () => {
  // 保证表单项校验通过再发请求
  loading.value = true
  try {
    await useStore.userLogin(loginForm)
    // 判断登录时候是否有query参数
    let redirect: any = route.query.redirect
    router.push({ path: redirect || '/' })
    ElNotification({
      type: 'success',
      message: '登录成功',
      title: `HI, ${getTime()}好`,
    })
  } catch (error) {
    // 登录失败的提示信息
    ElNotification({ type: 'error', message: (error as Error).message })
  } finally {
    loading.value = false
  }
}
// 自定义校验规则函数
const valudatorUserName = (rule: any, value: any, callback: any) => {
  // rule 为校验的对象, value为表单元素文本内容 callback: 符合条件直接调用,不符合注入错误提示信息.
  if (/^\d{5,10}$/.test(value)) {
    callback()
  } else {
    callback(new Error('账号长度至少五位最多十位'))
  }
}
// 自定义校验规则函数
const valudatorPassword = (rule: any, value: any, callback: any) => {
  // rule 为校验的对象, value为表单元素文本内容 callback: 符合条件直接调用,不符合注入错误提示信息.
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码长度至少六'))
  }
}
//定义表单校验规则
const rules = {
  username: [
    // { required: true, message: '用户名不能为空', trigger: 'blur' },
    // {
    //   min: 6,
    //   max: 10,
    //   message: '账号长度在六到十位',
    //   trigger: 'change',
    // },
    {
      validator: valudatorUserName,
      trigger: 'change',
    },
  ],
  password: [
    {
      validator: valudatorPassword,
      trigger: 'change',
    },
  ],
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
}

.login-form {
  position: relative;
  padding: 40px;
  width: 80%;
  top: 30vh;
  background: url('@/assets/images/login_form.png') no-repeat;
  background-size: cover;

  h1 {
    font-size: 40px;
    color: white;
  }
  h2 {
    margin: 20px 0;
    color: white;
  }
}

.login-btn {
  width: 100%;
}
</style>
